<?php

class Tarea_IndexController extends Zend_Controller_Action {

    public function init() {
        /* I  nitialize action controller here */
    }

    
    // ver todas las tareas
    public function viewAction() {
        $row = new Tarea_Model_DbTable_Tarea(); 
        $row = $row->fetchAll();
        $this->view->row = $row;
    }

    
    //todas las tarea pertencientes al usuario en sesion *//////////////////
    public function mytaskAction() {     
        $auth = Zend_Auth::getInstance();
        $user = $auth->getIdentity()->id;
        $row = new Tarea_Model_DbTable_Tarea();   
        $row = $row->fetchAll("id_user_assigned = '$user'");
        $this->view->row = $row;
    }

    // ver una sola tarea debe tener un id en la url ////////////////////
    public function viewtareaAction() {
        $id = $this->_getParam('id', 0);
        $fin = $this->_getParam('fin', 'none');
        if (!is_numeric($id) or $id <= 0) {
            $this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Solicitud no valida.');
            $this->_helper->redirector->gotoUrlAndExit('/tarea/index/view');
        }
        $tarea = new Tarea_Model_DbTable_Tarea();
        if ($fin == 'fin') {
            $tarea->update(array('status' => 1), "id = '$id'");
            $this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Tarea completada.');
            $this->_helper->redirector->gotoUrlAndExit('/tarea/index/view');
        }
        $this->view->tarea = $tarea->fetchRow("id = '$id'");
    }

    public function deleteAction() {
        $id = $this->_getParam('id', 0);
        $tarea = new Tarea_Model_DbTable_Tarea();
        $tarea->remove((int) $id);
        $this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Tarea eliminada con exito.');
        $this->_helper->redirector->gotoUrlAndExit('/tarea/index/view');
    }

    public function addtareaAction() {
        $id_alcance = $this->_getParam('id', 'none');
        $form = new Tarea_Form_Add();
        if (!is_numeric($id_alcance) or $id_alcance <= 0) {
            $this->view->m = 'h';
        } else {
            $title = new Tarea_Model_DbTable_Alcanse();
            $this->view->title = $title->getTitle($id_alcance);
        }
        $elem_alc = $form->getElement('id_alcance');
        $alc = new Alcance_Model_DbTable_Alcanse();
        $alc = $alc->getCombo();
        $elem_alc->addMultiOptions($alc);
        $form->setAction($this->getRequest()->getRequestUri());
        $elem = $form->getElement('id_user_assigned');
        $user_assigned = new User_Model_DbTable_User();
        $user_assigned = $user_assigned->getCombo();
        $elem->addMultiOptions($user_assigned);
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                unset($formData['submit']);
                $auth = Zend_Auth::getInstance();
                $iduser = $auth->getIdentity()->id;
                $formData['id_user_create'] = $iduser;
                $formData['id_alcance'] = $id_alcance;
                $formData['end_date'] = date('Y-m-d', strtotime($formData['end_date']));
                $formData['create_date'] = date('Y-m-d', strtotime($formData['create_date']));
                $userForm = new Tarea_Model_DbTable_Tarea();
                $userForm->add($formData);
                $this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Tarea adicionada con exito.');
                $this->_helper->redirector->gotoUrlAndExit('/tarea/index/addtarea/id/' . $id_alcance);
            } else {
                $form->populate($formData);
                $this->view->form = $form;
            }
        } else {
            $this->view->form = $form;
        }
    }

    public function editAction() {
        $id = $this->_getParam('id', 'none');
        if (!is_numeric($id) or $id <= 0) {
            $this->view->m = '';
        }
        $form = new Tarea_Form_Add();
        $elem_alc = $form->getElement('id_alcance');
        $alc = new Alcance_Model_DbTable_Alcanse();
        $alc = $alc->getCombo();
        $elem_alc->addMultiOptions($alc);
        $elem = $form->getElement('id_user_assigned');
        $user_assigned = new User_Model_DbTable_User();
        $user_assigned = $user_assigned->getCombo();
        $elem->addMultiOptions($user_assigned);
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                unset($formData['submit']);
                $userForm = new Tarea_Model_DbTable_Tarea();
                $formData['update_date'] = date('Y-m-d');
                $formData['end_date'] = date('Y-m-d', strtotime($formData['end_date']));
                $formData['create_date'] = date('Y-m-d', strtotime($formData['create_date']));
                $userForm->edit($formData, $id);
                $this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Tarea Modificada con exito.');
                $this->_helper->redirector->gotoUrlAndExit('/tarea/index/view');
            } else {
                $form->populate($formData);
                $this->view->form = $form;
            }
        } else {
            $formData = new Tarea_Model_DbTable_Tarea();
            $formData = $formData->fetchRow("id=" . $id)->toArray();
            $formData['end_date'] = date('d-m-Y', strtotime($formData['end_date']));
            $formData['create_date'] = date('d-m-Y', strtotime($formData['create_date']));
            $form->populate($formData);
            $this->view->form = $form;
        }
    }

    function es_int($numero) {
        if (preg_match("/^(+|-)?[0-9]+$/", $numero))
            return true;
        else
            return false;
    }

}

?>
